package electionView;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.concurrent.Semaphore;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import electionCtrl.Election;

public class PasswordGui extends ElectionGui{
	
	public enum Button
	{
		OK,CANCEL;
		
		public String toString()
		{
			String str = super.toString();
			return str.substring(0,1) + str.substring(1).toLowerCase().replaceAll("_", " ");
		}
	};
	
	private JTextField usernameField;
	private JPasswordField passwordField;
	private JButton okay;
	private JButton cancel;
	private Button action;
	private static final int FIELD_SIZE = 34;
	
	public PasswordGui(Election election, Semaphore semaphore)
	{
		super(election, new JFrame("Administrator Check"), semaphore);
		this.mainFrame.setLayout(new FlowLayout());
		this.mainFrame.setSize(400, 130);
		
		this.usernameField = new JTextField("Username",FIELD_SIZE);
		this.usernameField.setToolTipText("Enter an Admin username");
		this.usernameField.addFocusListener(new UsernameFieldKeyboardFocusListener());
		
		this.passwordField = new JPasswordField("Password",FIELD_SIZE);
		this.passwordField.setToolTipText("Enter the corresponding password");
		this.passwordField.addFocusListener(new PasswordFieldKeyboardFocusListener());
		this.passwordField.addKeyListener(new EnterKeyListener());
		
		this.okay = new JButton(Button.OK.toString());
		this.okay.addActionListener(new OkayButtonListener());
		
		this.cancel = new JButton(Button.CANCEL.toString());
		this.cancel.addActionListener(new CancelButtonListener());
		
		this.mainFrame.add(this.usernameField);
		this.mainFrame.add(this.passwordField);
		this.mainFrame.add(this.okay);
		this.mainFrame.add(this.cancel);
		
		this.action = null;
	}
	
	private class OkayButtonListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e) 
		{
			action = Button.OK;
			releaseResources();
		}
	}
	
	private class CancelButtonListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e) 
		{
			clearUsernameAndPassword();
			action = Button.CANCEL;
			releaseResources();
		}
	}
	
	private class EnterKeyListener implements KeyListener
	{
		@Override
		public void keyPressed(KeyEvent e)
		{
			int id = e.getKeyCode();
			if(id == KeyEvent.VK_ENTER)
			{
				action = Button.OK;
				releaseResources();
			}
		}
		
		@Override
		public void keyReleased(KeyEvent e)
		{}
		@Override
		public void keyTyped(KeyEvent e)
		{}
	}
	
	private class UsernameFieldKeyboardFocusListener implements FocusListener
	{
		@Override
		public void focusGained(FocusEvent e)
		{
			if(usernameField.getText().equals("Username"))
			{
				usernameField.setText(null);
			}
		}

		@Override
		public void focusLost(FocusEvent e)
		{
//			if(usernameField.getText() == null || usernameField.getText().equals(""))
//			{
//				usernameField.setText("Username");
//			}
		}
	}
	
	private class PasswordFieldKeyboardFocusListener implements FocusListener
	{
		@Override
		public void focusGained(FocusEvent e)
		{
			String text = new String(passwordField.getPassword());
			if(text.equals("Password"))
			{
				passwordField.setText(null);
			}
		}

		@Override
		public void focusLost(FocusEvent e)
		{
//			String text = new String(passwordField.getPassword());
//			if(text == null || text.equals(""))
//			{
//				passwordField.setText("Password");
//			}
		}
	}
	
	public String getUsername()
	{
		return this.usernameField.getText();
	}
	
	public String getPassword()
	{
		return new String(this.passwordField.getPassword());
	}
	
	//index 0: username
	//index 1: password
	public String[] getUsernameAndPassword()
	{
		String [] userPass = new String [2];
		userPass[0] = this.getUsername();
		userPass[1] = this.getPassword();
		this.clearUsernameAndPassword();
		return userPass;
	}
	
	public void clearUsernameAndPassword()
	{
		this.usernameField.setText("Username");
		this.passwordField.setText("Password");
	}
	
	public Button waitForAction() throws InterruptedException
	{
		this.acquireResources();
		Button b = this.action;
		this.action = null;
		return b;
	}
}
